<template>
  <div>
    <!-- table -->
    <a-card style="margin-top: 24px">
      <div class="table-operator">
        <div style="margin-bottom:15px;">
          <a-button style="margin-left:15px;" type="primary" icon="plus" @click="handleClickAdd">{{ $t('bannerList.xj') }}</a-button>
        </div>
      </div>
      <a-table
        :columns="columns"
        :dataSource="data"
        :pagination="false"
        :loading="dataLoading"
      >
        <template slot="imgs" slot-scope="text">
          <template>
            <div v-if="text">
              <img style="margin-right:5px;" v-for="item in text.split(',')" @click="handleClickReview($hostServer+item)" class="table-img" :src="$hostServer+item" alt=""/>
            </div>
            <div v-else>-</div>
          </template>
        </template>
        <template slot="operation" slot-scope="text, record">
          <span>
            <a-popconfirm title="$t('bannerList.sfysccx')" @confirm="handleClickDelete(record.id)">
              <a style="color:red">{{ $t('bannerList.sc') }}</a>
            </a-popconfirm>
          </span>
        </template>
      </a-table>
    </a-card>
    <create-form ref="createModal" @ok="handleAddOk" />
    <DialogImg :img_url="curr_img_url" :is_show="curr_img_show"/>
  </div>
</template>

<script>
import { mixin, mixinDevice } from '@/utils/mixin'
import CreateForm from './CreateForm'
import DialogImg from '../../../components/DialogImg'

export default {
  name: 'content_bind',
  mixins: [mixin, mixinDevice],
  components: {
    CreateForm,
    DialogImg
  },
  data () {
    return {
      description: this.$t('bannerList.xszqddhfgg'),
      loading: false,
      dataLoading: false,
      curr_img_url: '',
      curr_img_show: false,
      // table
      columns: [
        {
          title: this.$t('bannerList.lx'),
          dataIndex: 'belong',
          key: 'belong',
          scopedSlots: { customRender: 'belong' }
        },
        {
          title: this.$t('bannerList.ym'),
          dataIndex: 'page',
          key: 'page',
          scopedSlots: { customRender: 'page' }
        },
        {
          title: this.$t('bannerList.tp'),
          dataIndex: 'imgs',
          key: 'imgs',
          scopedSlots: { customRender: 'imgs' }
        },
        {
          title: this.$t('bannerList.cz'),
          key: 'action',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      data: [],
      errors: [],
      waitCnt: 0,
      succCnt: 0,
      failCnt: 0,
      showBindInfo: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    handleClickHideBindInfo () {
      this.showBindInfo = false
    },
    handleClickAdd () {
      this.$refs.createModal.add()
    },
    handleAddOk (form) {
      this.$http.post('/banner/add', this.$qs.stringify(form)).then(res => {
        if (res.code === 0) {
          this.getList()
          this.$notification.success({
            message: this.$t('bannerList.cg'),
            description: res.msg
          })
        } else {
          this.$notification.error({
            message: this.$t('bannerList.sb'),
            description: res.msg
          })
        }
      }).catch(err => {
        this.$notification.error({
          message: this.$t('bannerList.sb'),
          description: err.message
        })
      })
    },
    getList (type) {
      let url = '/banner/list'
      if (type !== undefined) {
        url += '?type=' + type
      }
      this.$http.get(url).then(res => {
        this.data = res
        this.waitCnt = 0
        this.succCnt = 0
        this.failCnt = 0
        this.data.forEach(item => {
          if (item.status === 0) {
            this.waitCnt++
          } else if (item.status === 1) {
            this.succCnt++
          } else {
            this.FailCnt++
          }
        })
      }).catch(err => {
        this.$notification.error({
          message: this.$t('bannerList.sb'),
          description: err.message
        })
      })
    },
    handleClickDelete (id) {
      this.$http.post('/banner/delete', this.$qs.stringify({
        id: id
      })).then(res => {
        const newData = this.data.filter(item => item.id !== id)
        this.data = newData
      }).catch(err => {
        this.$notification.error({
          message: this.$t('bannerList.sb'),
          description: err.message
        })
      })
    },
    handleChangeType (e) {
      this.getList(e.target.value)
    },
    handleClickReview (url) {
      this.curr_img_url = url
      this.curr_img_show = !this.curr_img_show
    }
  }
}
</script>

<style lang="less" scoped>
  .card{
    margin-bottom: 24px;
  }
  .douyin-bind-img{
    margin-bottom:10px;
    text-align: center;
  }
  .douyin-bind-img img{
    width:100px;
    height:100px;
    border-radius: 50%;
  }
</style>
